package com.zzp.basepresenter.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.zzp.basepresenter.R;


/**
 * Created by HMY
 */
public class ImageLoaderUtil {

    public static ImageLoader getImageLoader() {
        return ImageLoader.getInstance();
    }

    public static DisplayImageOptions getPhotoImageOption() {
        Integer extra = 1;
        DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true)
                .showImageOnLoading(R.drawable.style_t80_solid_layout)
                .extraForDownloader(extra)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565).build();
        return options;
    }

    public static DisplayImageOptions getCircleImageOption(Context context) {
        Integer extra = 1;
        DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true)
                .showImageOnLoading(R.drawable.shape_image_loading_5dp)
                .extraForDownloader(extra)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .displayer(new RoundedBitmapDisplayer(DensityUtil.dip2px(context, 5)))
                .bitmapConfig(Bitmap.Config.RGB_565).build();
        return options;
    }

    public static DisplayImageOptions getCircleImageOption(Context context, int radius) {
        Integer extra = 1;
        DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true)
                .extraForDownloader(extra)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .displayer(new RoundedBitmapDisplayer(DensityUtil.dip2px(context, radius)))
                .bitmapConfig(Bitmap.Config.RGB_565).build();
        return options;
    }

    public static DisplayImageOptions getCircleLogoOption(int radius) {
        Integer extra = 1;
        DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true)
                .extraForDownloader(extra)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .displayer(new RoundedBitmapDisplayer(radius))
                .bitmapConfig(Bitmap.Config.RGB_565).build();
        return options;
    }

    public static DisplayImageOptions getPhotoLogoImageOption() {
        Integer extra = 1;
        DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true)
                // .showStubImage(R.mipmap.banner_default)
                .extraForDownloader(extra)
                .bitmapConfig(Bitmap.Config.RGB_565).build();
        return options;
    }

    public static void displayImage(ImageView imageView, String url, DisplayImageOptions options) {
        getImageLoader().displayImage(url, imageView, options, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {

            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap bitmap) {

            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {

            }
        });
    }

    public static void displayImage(Context context, ImageView imageView, String url, DisplayImageOptions options, ImageLoadingListener listener) {
        getImageLoader().displayImage(url, imageView, options, listener);
    }

    public static void displayImage(String url, DisplayImageOptions options, ImageLoadingListener listener) {
        getImageLoader().loadImage(url, options, listener);
    }
}
